import {compatibleStorage} from '../utils'
//#ifdef H5
let storageExtraParam = sessionStorage.getItem("kol-eoc-extraParam")
if (storageExtraParam) { // kol的埋点。存缓存是个字符串对象。先判断是不是字符串对象
	if (storageExtraParam.startsWith('{')) {
		storageExtraParam = JSON.parse(storageExtraParam)
	} else {
		storageExtraParam = {}
		sessionStorage.removeItem("kol-eoc-extraParam")
	}
}
//#endif
const buryPoint = {
	namespaced:true,
	state:{
		cookiesId: compatibleStorage('cookiesId') || "",
		sxOrientations: 'portrait', // 视讯横竖屏
		//#ifdef H5
		sxUdid: compatibleStorage('sxUdid', true) || "",
		sessionId: sessionStorage.getItem("sessionIdForStat") || '',
		shortId: compatibleStorage("shortId", true) || '',
		ydOpenId: sessionStorage.getItem("ydOpenId") || '', // 悦动传过来带在链接上的openId
		extraParam: storageExtraParam || {}, // kol直播的场景、主播埋点
		//#endif

		//#ifdef MP-WEIXIN
		sessionId:'',
		shortId:'',
		//#endif
	},
	mutations:{
		setExtraParam(state,info) {
			state.extraParam = info;
			//#ifdef H5
			sessionStorage.setItem('kol-eoc-extraParam', JSON.stringify(info))
			//#endif
		},
		setCookiesId(state,info){
			state.cookiesId = info;
			uni.setStorageSync('cookiesId',info);
		},
		setSessionId(state,info){
			state.sessionId = info;
			//#ifdef H5
			sessionStorage.setItem('sessionIdForStat',info)
			//#endif
		},
		setSxUdid(state,info){
			state.sxUdid = info;
			//#ifdef H5
			sessionStorage.setItem('sxUdid',info)
			//#endif
		},
		setSxOrientations(state, info) {
			state.sxOrientations = info
		},
		setShortId(state,info){
			state.shortId = info;
			//#ifdef H5
			sessionStorage.setItem('shortId',info)
			//#endif
		},
		//#ifdef H5
		setYdOpenId(state, info){
			state.ydOpenId = info;
			sessionStorage.setItem('ydOpenId', info)
		}
		//#endif
	},
	getters: {
		extraParamStr(state) {
			const extraParam = state.extraParam
			let extraStr = ''
			Object.keys(extraParam).forEach(key => {
				const sign = extraStr ? ';' : ''
				extraStr += `${sign}${key}=${extraParam[key]}`
			})
			return extraStr
		}
	}
}

export default buryPoint
